#include <stdlib.h>
#include "QUEUE.h"
/**
 * Program 4.11 FIFO queue array implementation
 * --------------------------------------------
 * 数组+头索引+尾索引
 * 
 * 问：如何实现put操作？
 * 答：添加到队尾，递增尾索引
 * 
 * 问：如何实现get操作？
 * 答：从队头移除元素，递增头索引
 */

static Item* q;
static int N;
static int head, tail;

void QUEUEinit(int maxN){
    N = maxN + 1;
    q = malloc(N * sizeof(Item));
    head = N;
    tail = 0;
}

int QUEUEempty(){
    return head % N == tail;
}

void QUEUEput(Item item){
    q[tail] = item;
    tail++;
    tail = tail%N;
}

Item QUEUEget(){
    head = head % N;
    Item item = q[head];
    head++;
    return item;
}

